from config import *
from common import *
import sys
import time
import simplejson as json
import stomp
from monitor import Monitor
from peer import Peer
from recipe_server import RecipeServer
from tracker import Tracker
from threading import Thread
import traceback

class Controller:
    peers = []
    trackers = []
    recipe_servers = []
    monitors = []
    log_file = LogFile()
    def web_publish(self):
        return {'peers':self.get_peers(),
                'trackers':self.get_trackers(),
                'recipe_servers':self.get_recipe_servers(),
                'monitors':self.get_monitors(),
                'logs': self.log_file.get_last_messages()[:10]
                }
    def stop_threads(self):
        for p in self.peers:
            p.stop()
        for t in self.trackers:
            t.stop()
        for m in self.monitors:
            m.stop() 
        for r in self.recipe_servers:
            r.stop()
    def _run(self, arr, klass, **args):
        args = self.fix_arg_type(args)       
        mem = klass(**args)
        arr.append(mem)
        mem.start()
        mem.setName('%s-%s' % (klass.__name__, len(arr)))
        #web_server.register(mem)    
    def run_peer(self, **args):
        self._run(self.peers, Peer, **args)        
                            
    def run_recipe_server(self, **args):
        self._run(self.recipe_servers, RecipeServer, **args)
            
    def run_tracker(self, **args):
        self._run(self.trackers, Tracker, **args)            
    
    def run_monitor(self, **args):        
        self._run(self.monitors, Monitor, **args)       
    
    def _stop(self, arr, ind):
        arr[int(ind)].stop()
        arr[int(ind)].join()
    def stop_peer(self, ind):        
        self._stop(self.peers, ind)        
        
    def stop_recipe_server(self, ind):
        self._stop(self.recipe_servers, ind)        
    
    def stop_monitor(self, ind):
        self._stop(self.monitors, ind)        
    
    def stop_tracker(self, ind):        
        self._stop(self.trackers, ind)        
    
    def _get(self, arr):
        if not arr:
            return []
        return [{'http_port':x.http_port,
                'external_ip' : x.external_ip,
                'external_port': x.external_port,
                'name' : x.getName(),
                'isAlive' : x.isAlive(),
                } for x in arr]
    def get_peers(self):
        return self._get(self.peers)
    def get_recipe_servers(self):
        return self._get(self.recipe_servers)
    def get_monitors(self):
        return self._get(self.monitors)
    def get_trackers(self):
        return self._get(self.trackers)
    def start(self):
        stomper = StompListener(self)  
        stomper.start() 
    def fix_arg_type(self,args):
        m = {}
        for key, val in args.iteritems():
            if key in ('rs_port', 'tracker_port', 'http_port', 'rpc_port', 'external_port','ind'):                       
                m[str(key)] = int(val)
            else:
                m[str(key)] = str(val)
        return m
     
